﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Kugar.Core.ExtMethod;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.Helpers
{
    public class TagAttrbute
    {
        private Dictionary<string, string> _attr = null;// new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);

        public TagAttrbute()
        {
            _attr = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
        }

        public TagAttrbute(Dictionary<string, string> attr)
        {
            _attr = new Dictionary<string, string>(attr, StringComparer.CurrentCultureIgnoreCase);
        }

        public static TagAttrbute Build(string name, string value)
        {
            return new TagAttrbute().SetOrReplaceAttr(name, value);
        }

        public TagAttrbute Style(string styleName, string value)
        {
            MergeAttr("style", $"{styleName}:{value}");

            return this;
        }

        /// <summary>
        /// 设置一个属性值,如果已存在,则合并
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public TagAttrbute MergeAttr(string name, string value)
        {
            name = name.Trim();

            var oldValue = _attr.TryGetValue(name, "");

            string v = "";

            if (name.CompareTo("style", true))
            {
                v = $"{oldValue};{value}".Trim();
            }
            else
            {
                v = $"{oldValue} {value}".Trim();
            }

            _attr.AddOrUpdate(name, v);

            return this;
        }

        /// <summary>
        /// 设置一个属性值,如果已存在,则直接替换
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public TagAttrbute SetOrReplaceAttr(string name, string value)
        {
            _attr.AddOrUpdate(name, value);

            return this;
        }

        public Dictionary<string, string> Attributes { get { return _attr; } }

        public TagAttrbute Class(string className)
        {
            return MergeAttr("class", className);
        }

        public TagAttrbute ID(string id)
        {
            SetOrReplaceAttr("id", id);
            return this;
        }

        public TagAttrbute Data(string dataName, string value)
        {
            SetOrReplaceAttr(dataName, value);

            return this;
        }

        public void ApplyToTagBuilder(TagBuilder builder)
        {
            foreach (var attr in Attributes)
            {
                builder.MergeAttribute(attr.Key, attr.Value);
            }
        }

        public string GetValue(string key, string defalutValue = "")
        {
            return _attr.TryGetValue(key, defalutValue);
        }

        public void Combine(TagAttrbute attr)
        {
            foreach (var a in attr.Attributes)
            {
                this.MergeAttr(a.Key, a.Value);
            }
        }

        public static implicit operator Dictionary<string, string>(TagAttrbute tag)
        {
            return tag.Attributes;
        }

        public static implicit operator TagAttrbute(Dictionary<string, string> tag)
        {
            return new TagAttrbute(tag);
        }



        public bool HasAttrs()
        {
            return _attr.HasData();
        }

        public override string ToString()
        {
            return _attr.Where(x=>!string.IsNullOrWhiteSpace(x.Value)).Select(x => $"{x.Key}=\"{x.Value}\"").JoinToString(' ').Trim(' ');
        }
    }
}
